var loginStatus;
var userName;

function disableAddBtn() {
	$('#addBtn').attr('disabled',true);
	$('#addBtn').css('opacity',0.7);
	$('#addBtn').css('color', '#747474');
};

function enableAddBtn() {
	$('#addBtn').attr('disabled',false);
	$('#addBtn').css('opacity',1);
	$('#addBtn').css('color', '#5A1F4B');
};

$('#resetBtn').click(function(){
	disableAddBtn();
	$('#word-text').val('');
	$('#meaning-text').val('');
	$('#sentence-text').val('');
});

$('#addBtn').click(function(){

	$.post('scripts/addword.php',{word:$('#word-text').val(), mean:$('#meaning-text').val(), sent:$('#sentence-text').val()}, function(data){
		$('#addWordStatus').fadeTo(0,1);
		$('#addWordStatus').html(data);
		$('#addWordStatus').fadeTo(5000,0);
		disableAddBtn();
		$('#word-text').val('');
		$('#meaning-text').val('');
		$('#sentence-text').val('');
	});
	
});

$('#word-text').keyup( function() {
	
	if($(this).val().length > 0 && $('#meaning-text').val().length > 0 && $('#sentence-text').val().length > 0 ){
		enableAddBtn();
	}else{
		disableAddBtn();
	}
});

$('#meaning-text').keyup( function() {
	if($('#word-text').val().length > 0 && $(this).val().length > 0 && $('#sentence-text').val().length > 0 ){
		enableAddBtn();
	}else{
		disableAddBtn();
	}
});

$('#sentence-text').keyup( function() {
	if($('#word-text').val().length > 0 && $('#meaning-text').val().length > 0 && $(this).val().length > 0 ){
		enableAddBtn();
	}else{
		disableAddBtn();
	}
});

$('#submitBtn').click(function(){
$.post('scripts/login.php', { username : $('#username').val(), password: $('#password').val() }, function(data){
	if(data != 'fail'){
		$('#loginvalidator').hide();	
		userName = data;
		$('#username').val('');
		$('#password').val('');
		$('#login-wrapper').fadeTo(200,0,function(){
		$('#login-wrapper').css('z-index',-1);	
		$('#body-wrapper').fadeTo(200,1);		
		$('#body-wrapper').css('z-index',2);
		$('.userdisp').show();
		$('#user').text(userName);
		});
	}else{
		$('#loginvalidator').show();
	}
});
});

$('#onlineMeaning').click( function(){
	if($('#word-text').val().length > 0){
		var link = 'http://www.thefreedictionary.com/' + $('#word-text').val();	
		$('#onlineMeaning a').attr('href', link);
	}else{
		$('#onlineMeaning a').attr('href', '');
		$('#onlineMeaning a').removeAttr('href');
	}
});

$(document).ready(function () {

$('#word-text').watermark('Write the WORD here.');
$('#meaning-text').watermark('Write the MEANING here.');
$('#sentence-text').watermark('Here you must enter a SENTENCE that is relevant to the context of the usage of the word entered above.');

disableAddBtn();

$.post('scripts/login.php', function(data){
	if(data != 'fail' && data != ''){
		loginStatus = 1;
		userName = data;
	}else{
		loginStatus = 0;
	}	
	
	if(loginStatus == 0){
		$('#body-wrapper').fadeTo(500,0.3,function(){	
		$('#login-wrapper').css('z-index',2);	
		$('#login-wrapper').fadeTo(1500,1);
		});
	}else{
		$('#login-wrapper').css('z-index',-1);	
		$('#body-wrapper').fadeTo(200,1);		
		$('#body-wrapper').css('z-index',2);
		$('.userdisp').show();
		$('#user').text(userName);
	}
});

});
